package com.Android.Intr;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class IntrActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //Autologin stuff
        //0 refers to type of mode
        final CheckBox checkbox = (CheckBox) findViewById(R.id.login_checkbox);
        final SharedPreferences settings = getSharedPreferences("PREF", 0);
        if(settings.getBoolean("Authenticated", false)){
        	Intent toHome = new Intent(IntrActivity.this, MainFrame.class);
			startActivity(toHome);
        }
        
        Button login = (Button) findViewById(R.id.login);
        final EditText uName = (EditText) findViewById(R.id.et_username);
        final EditText pWord = (EditText) findViewById(R.id.et_password);
        login.setOnClickListener(new View.OnClickListener() {	
        	
			public void onClick(View v) {
				if(autheticate(uName.getText().toString(), pWord.getText().toString())){
					SharedPreferences.Editor editor = settings.edit();
					if(checkbox.isChecked()){
						//stores that you want to stay logged in
						editor.putBoolean("Authenticated", true);
						editor.putString("Password", pWord.getText().toString());
					}
					editor.putString("Username", uName.getText().toString());
					editor.commit();
					Intent toHome = new Intent(IntrActivity.this, MainFrame.class);
					startActivity(toHome);
				}
				else{
					Toast toast = Toast.makeText(getApplicationContext(), "Wrong username/password combination", Toast.LENGTH_SHORT);
			        toast.show();
				}
			}
		});	
    }
    
    //Currently a dummy method
    public boolean autheticate(String u, String p){
    		if(u.equals("admin") && p.equals("password")){
    			return true;
    		}
		return false;
    }
    
    
    @Override
	protected void onStop(){
		super.onStop();
		this.finish();
	}
}